package guilayer;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import modellayer.Order;
import controllayer.OrderCtrl;

public class OrderList extends JPanel {

	private JScrollPane scrollPane;
	private DefaultTableModel tableModel;
	//private CustomerCtrl customerCtrl;
	private OrderCtrl orderCtrl;
	private JTable table;
	protected String tableSelectedCustomerNo;

	/**
	 * Create the panel.
	 */
	public OrderList() {
		orderCtrl = OrderCtrl.getInstance();
		setLayout(new BorderLayout(0, 0));
		
		scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		createAndSetTable();
		
	}
	
	private void createAndSetTable() {
		String[] columnNames = { "Ordernr.", "Ekspedient", "Kunde", "Dato" };
		tableModel = new DefaultTableModel(null, columnNames);
		
		for(Order order : orderCtrl.getOrderList()) {
			
			String[] rowData = { order.getOrderNo(), order.getClerk().getFullname(), order.getCustomer().getFullname(), order.getDate() };
			
			tableModel.addRow(rowData);
		}
		
		table = new JTable(tableModel) {  
				public boolean isCellEditable(int row,int column){  
					return false;  
				}
			};
			
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				JTable target = (JTable)e.getSource();
		         
		         int row = target.getSelectedRow();
		         
		         tableSelectedCustomerNo = (String) table.getValueAt(row, 0); // Getting the data which is in the "row" number and column 0 (the first).
		         
		         showBottomButtons(true);
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			      if (e.getClickCount() == 2) {
			         JTable target = (JTable)e.getSource();
			         
			         int row = target.getSelectedRow();
			         
			         String customerNo = (String) table.getValueAt(row, 0); // Getting the data which is in the "row" number and column 0 (the first).
			         
			         callRootPane(1, 12, customerNo);
			      }
			   }
			});
		
		scrollPane.setViewportView(table);
	}

	protected void callRootPane(int i, int j, String customerNo) {
		// TODO Auto-generated method stub
		
	}

	protected void showBottomButtons(boolean b) {
		// TODO Auto-generated method stub
		
	}

}
